﻿using NationalInstruments.DAQmx;
using ScottPlot.Drawing.Colormaps;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Controls;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Models;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Properties;

namespace 低频天线阵列相控阵信号发生系统SmartFASK12.View.AdjustDemodulation.Views
{
    public partial class Uc_Modulation : UserControl
    {
        /// <summary>
        /// 显示数字信号
        /// </summary>
        public event EventHandler<string> HasShowNumberSignal;
        /// <summary>
        /// 是否启动
        /// </summary>
        private bool _isStart_Export = false;
        public bool IsStart_Export
        {
            get => _isStart_Export;
            set
            {
                _isStart_Export = value;
                if (_isStart_Export)
                {
                    btn_StartExecution.Enabled = false;
                    btn_ContinuousLaunch.Enabled = false;
                    btn_SendOnce.Enabled = false;
                    btn_Stop.Enabled = true;
                    panel_status_Export.BackgroundImage = Resources.green_36;
                }
                else
                {
                    btn_StartExecution.Enabled = true;
                    btn_ContinuousLaunch.Enabled = true;
                    btn_SendOnce.Enabled = true;
                    btn_Stop.Enabled = false;
                    panel_status_Export.BackgroundImage = Resources.gray_36;
                }
            }
        }
        /// <summary>
        /// 信号类型
        /// </summary>
        private int _signalType_modulate;
        public int SignalTypeModulate
        {
            get => _signalType_modulate;
            set
            {
                _signalType_modulate = value;
                if (_signalType_modulate == 0)
                {
                    button_random.BackColor = Color.White;
                    button_01.BackColor = Color.White;
                    button_10.BackColor = Color.Lime;
                    btn_ManualInput.BackColor = Color.White;
                }
                else if (_signalType_modulate == 1)
                {
                    button_random.BackColor = Color.White;
                    button_01.BackColor = Color.Lime;
                    button_10.BackColor = Color.White;
                    btn_ManualInput.BackColor = Color.White;
                }
                else if (_signalType_modulate == 2)
                {
                    button_random.BackColor = Color.Lime;
                    button_01.BackColor = Color.White;
                    button_10.BackColor = Color.White;
                    btn_ManualInput.BackColor = Color.White;
                }
                else if (_signalType_modulate == 3)
                {
                    button_random.BackColor = Color.White;
                    button_01.BackColor = Color.White;
                    button_10.BackColor = Color.White;
                    btn_ManualInput.BackColor = Color.Lime;
                }
            }
        }
        public Uc_Modulation()
        {
            InitializeComponent();

            HasShowNumberSignal += Uc_Modulation_HasShowNumberSignal;

            physicalChannelComboBox2.Items.AddRange(MNI.Singleton().GetChannals(PhysicalChannelTypes.AO));
            if (physicalChannelComboBox2.Items.Count > 1)
                physicalChannelComboBox2.SelectedIndex = 1;

            physicalChannelComboBox1.Items.AddRange(MNI.Singleton().GetChannals(PhysicalChannelTypes.AO));
            if (physicalChannelComboBox1.Items.Count > 0)
                physicalChannelComboBox1.SelectedIndex = 0;

            #region 调制
            comboBox_CommissioningmodedemoModulation.SelectedIndex = MPublic.ConfigModel2.modulationMode;
            textBox_signalfrequency_modulate.Text = MPublic.ConfigModel2.signalfrequency_modulate;
            textBox_samplingrate_modulate.Text = MPublic.ConfigModel2.samplingrate_modulate;
            textBox_Carrierfrequency1_modulate.Text = MPublic.ConfigModel2.carrierfrequency1_modulate;
            textBox_Carrierfrequency2_modulate.Text = MPublic.ConfigModel2.carrierfrequency2_modulate;
            textBox_Amplitude1_modulate.Text = MPublic.ConfigModel2.amplitude1_modulate;
            textBox_Amplitude2_modulate.Text = MPublic.ConfigModel2.amplitude2_modulate;
            textBox_Showpoints_modulate.Text = MPublic.ConfigModel2.signalnumber;
            textBox_standard1_modulate.Text = MPublic.ConfigModel2.standard1_modulate;
            textBox_standard2_modulate.Text = MPublic.ConfigModel2.standard2_modulate;
            SignalTypeModulate = MPublic.ConfigModel2.signalType_modulate;
            //相位
            numericUpDown_phase1.Value = (decimal)MPublic.ConfigModel2.phase1;
            numericUpDown_phase2.Value = (decimal)MPublic.ConfigModel2.phase2;
            //采集卡
            comboBox_AcquisitionCard.SelectedIndex = MPublic.ConfigModel2.acquisitionCard;
            //占空比
            numericUpDown_DutyCycle.Value = (decimal)MPublic.ConfigModel2.dutyCycle * 100;
            #endregion
            #region 输出
            minimumNumeric.Value = (decimal)MPublic.ConfigModel2.minValue;
            maximumNumeric.Value = (decimal)MPublic.ConfigModel2.maxValue;
            #endregion
        }

        /// <summary>
        /// 显示数字信号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Uc_Modulation_HasShowNumberSignal(object sender, string e)
        {
            this.BeginInvoke(new Action(() =>
            {
                richTextBox_signalsequence.Text = e;
            }));
        }

        /// <summary>
        /// 开始执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_StartExecution_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(btn_StartExecution.Text) && SignalTypeModulate==3)
            {
                MessageBox.Show("信号序列不能为空！","提示");
                return;
            }
            Regex r = new Regex("[^0|1]");
            Match m = r.Match(richTextBox_signalsequence.Text);
            if (m.Success && SignalTypeModulate == 3)
            {
                //含有不是0或1的字符
                MessageBox.Show("当前输入参数有误！");
                return;
            }

            SaveConfig();

            MEvent.Singleton().EmitStartExecution();
        }

        /// <summary>
        /// 保存Config文件
        /// </summary>
        public void SaveConfig()
        {
            ConfigModel2 ConfigModel2 = MPublic.ConfigModel2;
            ConfigModel2.physicalChannel2 = physicalChannelComboBox1.Text;
            #region 调制

            ConfigModel2.dutyCycle = (double)numericUpDown_DutyCycle.Value / 100;
            ConfigModel2.modulationMode = comboBox_CommissioningmodedemoModulation.SelectedIndex;
            ConfigModel2.signalfrequency_modulate = textBox_signalfrequency_modulate.Text;
            ConfigModel2.samplingrate_modulate = textBox_samplingrate_modulate.Text;
            ConfigModel2.carrierfrequency1_modulate = textBox_Carrierfrequency1_modulate.Text;
            ConfigModel2.carrierfrequency2_modulate = textBox_Carrierfrequency2_modulate.Text;
            ConfigModel2.amplitude1_modulate = textBox_Amplitude1_modulate.Text;
            ConfigModel2.amplitude2_modulate = textBox_Amplitude2_modulate.Text;
            ConfigModel2.signalnumber = textBox_Showpoints_modulate.Text;
            ConfigModel2.standard1_modulate = textBox_standard1_modulate.Text;
            ConfigModel2.standard2_modulate = textBox_standard2_modulate.Text;
            ConfigModel2.signalType_modulate = SignalTypeModulate;
            #endregion
            #region 输出
            ConfigModel2.physicalChannel = physicalChannelComboBox2.Text;
            ConfigModel2.minValue = Convert.ToDouble(minimumNumeric.Value);
            ConfigModel2.maxValue = Convert.ToDouble(maximumNumeric.Value);
            #endregion
            //采集卡类型
            ConfigModel2.acquisitionCard = comboBox_AcquisitionCard.SelectedIndex;
            ConfigModel2.phase1 = (double)numericUpDown_phase1.Value;
            ConfigModel2.phase2 = (double)numericUpDown_phase2.Value;
            ConfigModel2.signalsequence = richTextBox_signalsequence.Text;
            MPublic.ConfigModel2 = ConfigModel2;
        }

        /// <summary>
        /// 显示数字信号
        /// </summary>
        /// <param name="comtaskId"></param>
        public void EmitShowNumberSignal(string str)
        {
            HasShowNumberSignal?.Invoke(this, str);
        }

        #region 信号类型
        private void button_random_Click(object sender, EventArgs e)
        {
            SignalTypeModulate = 2;
        }

        private void button_01_Click(object sender, EventArgs e)
        {
            SignalTypeModulate = 1;
        }

        private void button_10_Click(object sender, EventArgs e)
        {
            SignalTypeModulate = 0;
        }

        private void btn_ManualInput_Click(object sender, EventArgs e)
        {
            SignalTypeModulate = 3;
        }
        #endregion


        /// <summary>
        /// 发射一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SendOnce_Click(object sender, EventArgs e)
        {
            MEvent.Singleton().EmitSendVoltage(SampleQuantityMode.FiniteSamples);
        }

        /// <summary>
        /// 持续发射
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ContinuousLaunch_Click(object sender, EventArgs e)
        {
            if (SignalTypeModulate == 3)
            {
                double OutputTime = (double)MConvert.Singleton().KToInt(textBox_samplingrate_modulate.Text) / (double)MConvert.Singleton().KToInt(textBox_Showpoints_modulate.Text);
                double ManuallyInputQuantity = Math.Ceiling(MConvert.Singleton().KToInt(textBox_signalfrequency_modulate.Text) * OutputTime);
                if (richTextBox_signalsequence.Text.Length != ManuallyInputQuantity)
                {
                    MessageBox.Show("输入数量不匹配");
                    return;
                }
            }
            MEvent.Singleton().EmitSendVoltage(SampleQuantityMode.ContinuousSamples);
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Stop_Click(object sender, EventArgs e)
        {
            MNI2.Singleton().Stop();
            //修改状态
            IsStart_Export = false;
        }

        /// <summary>
        /// 信号序列手动输入更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void richTextBox_signalsequence_TextChanged(object sender, EventArgs e)
        {
            if (SignalTypeModulate != 3)
                return;

            Regex r = new Regex("[^0|1]");
            Match m = r.Match(richTextBox_signalsequence.Text);
            if (m.Success)
            {
                //含有不是0或1的字符
                MessageBox.Show("当前输入参数有误！");
                return;
            }
            else
            {
                //都是由0或1的字符
            }

            double OutputTime = (double)MConvert.Singleton().KToInt(textBox_samplingrate_modulate.Text) / (double)MConvert.Singleton().KToInt(textBox_Showpoints_modulate.Text);
            double ManuallyInputQuantity = Math.Ceiling(MConvert.Singleton().KToInt(textBox_signalfrequency_modulate.Text) * OutputTime);
            label_proportion.Text = richTextBox_signalsequence.Text.Length + "/" + ManuallyInputQuantity;
        }
    }
}
